<template>
  <div class="home">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="菜品名称">
        <el-input v-model="dishes.dishName"></el-input>
      </el-form-item>
      <el-form-item label="菜品价格">
        <el-input v-model="dishes.price"></el-input>
      </el-form-item>
      <el-form-item label="菜品分类">
        <el-select v-model="dishes.category.id" placeholder="请选择菜品分类">
          <!-- label是菜品分类名称， value是菜品分类id -->
          <el-option
            v-for="category in categoryList"
            :key="category.id"
            :label="category.dishesCategoryName"
            :value="category.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="菜品图片">
        <el-upload
          :on-success="uploaderSuccess"
          :file-list="fileList"
          action="http://localhost:8080/uploader/dishImage"
          list-type="picture-card"
        >
          <i slot="default" class="el-icon-plus"></i>
          <div slot="file" slot-scope="{ file }">
            <img
              class="el-upload-list__item-thumbnail"
              :src="dishes.picPath"
              alt=""
            />
            <span class="el-upload-list__item-actions">
              <span
                class="el-upload-list__item-preview"
                @click="handlePictureCardPreview(file)"
              >
                <i class="el-icon-zoom-in"></i>
              </span>
              <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleRemove(file)"
              >
                <i class="el-icon-delete"></i>
              </span>
            </span>
          </div>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveDish('ruleForm')">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      fileList : [],
      categoryList: [],
      dishes: {
        dishName: "",
        price: "",
        picPath: "",
        category: {
          id: null,
        },
      },
    };
  },
  created() {
    //判断url地址上面有没有参数
    this.init();
  },
  watch: {
    // 这个方法是 路由发生改变以后 会出发的方法
    $route(to,from) {
      this.init();
    },
  },
  methods: {
    //封装的方法，如果有参数就执行修改，没有参数就执行添加
    init() {
      //判断数据库里有没有这个id
      if (this.$route.params && this.$route.params.id) {
        this.dishes.id = this.$route.params.id;
        this.$http
          .post(
            "http://localhost:8080/dish/findDishOne/" + this.$route.params.id
          )
          .then((res) => {
            this.dishes = res.data.data.dishes;
            this.fileList = [{ name: "测试", url: this.dishes.picPath }];
          })
          .catch((err) => {
            this.$message.error("连接超时，请检查网络");
          });
      } else {
        this.fileList = [];
        this.dishes = {
          id: "",
          dishName: "",
          price: "",
          picPath: "",
          category: {
            id: null,
          },
        };
      }
      this.findCategoryAll();
    },
    // 查询所有菜品分类信息
    findCategoryAll() {
      this.$http
        .post("http://localhost:8080/category/all")
        .then((res) => {
          this.categoryList = res.data.data.categorys;
        })
        .catch((err) => {
          this.$message.error("连接超时，请检查网络");
        });
    },
    // 上传成功以后 图片回显 在这里绑定数据就可以回显
    uploaderSuccess(res, file) {
      this.dishes.picPath = res.data.imgPath;
    },
    //放大镜的函数
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //删除的函数
    handleRemove(file) {
      console.log(file);
    },
    //保存菜品
    saveDish() {
      this.$http
        .post("http://localhost:8080/dish/addDishes", this.dishes)
        .then((res) => {
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: "保存成功!",
            });
          } else {
            this.$message.error("保存菜品失败");
          }
          this.$router.push("/showDish");
        })
        .catch((err) => {
          this.$message.error("连接超时，请检查网络");
        });
    },
  },
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader >>> .el-upload {
  border: 1px dashed #dcdfe6;
}
</style>